import React from 'react';
import {putSofCashInsertDeduct} from '../../../../api'
import { Modal, Form, Input, InputNumber, DatePicker, message } from 'antd'
import moment from 'moment'

const { TextArea } = Input;

/**
 * @description: 添加扣款信息
 * @author: xiaoxujs
 * @updata: xiaoxujs（2021-07-28）创建
 */

const AddDeduction = ({ setAddDeductionShow, addDeductionShow, conNo, cashId, listFun }) => {
    const [form] = Form.useForm()
    const {validateFields, resetFields} = form
    const handleOk = () => {
        validateFields().then(values => {
            values.conNo = conNo
            values.cashId = cashId
            values.deductDate = moment(values.deductDate).format('YYYY-MM-DD')
            ;(async () => {
                const {code, msg} = await putSofCashInsertDeduct(values)
                if(code === '20000') {
                    message.success('添加扣款成功！')
                    resetFields()
                    listFun()
                    setAddDeductionShow(false);
                }else{
                    message.error(msg)
                }
            })()
        })
        
    };

    const handleCancel = () => {
        resetFields()
        setAddDeductionShow(false);
    };
    return <Modal title="添加扣款" visible={addDeductionShow} onOk={handleOk} onCancel={handleCancel}>
        <Form
            labelCol={{ span: 8 }}
            wrapperCol={{ span: 16 }}
            form = {form}
        >
            <Form.Item
                label="扣款原因"
                name="deductCause"
                rules={[{ required: true, message: '请输入扣款原因!' }]}
            >
                <Input />
            </Form.Item>
            <Form.Item
                label="扣款时间"
                name="deductDate"
                rules={[{ required: true, message: '请选择扣款时间!' }]}
            >
                <DatePicker />
            </Form.Item>
            <Form.Item
                label="扣款金额"
                name="deductMoney"
                rules={[{ required: true, message: '请输入扣款金额!' }]}
            >
                <InputNumber />
            </Form.Item>
            <Form.Item
                label="备注"
                name="remark"
            >
                <TextArea />
            </Form.Item>
        </Form>
    </Modal>
}

export default AddDeduction